﻿// 
// DotNetNuke?- http://www.dotnetnuke.com 
// Copyright (c) 2002-2011 
// by DotNetNuke Corporation 
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions: 
// 
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software. 
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE. 
// 

using System;
using System.Configuration;
using System.Data;
using System.Xml;
using System.Web;
using System.Collections.Generic;

using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Services.Search;
using DotNetNuke.Entities.Modules;

namespace ZenKee.Modules.ListPages
{

    /// ----------------------------------------------------------------------------- 
    /// <summary> 
    /// The Controller class for ListPages 
    /// </summary> 
    /// <remarks> 
    /// </remarks> 
    /// <history> 
    /// </history> 
    /// ----------------------------------------------------------------------------- 
    public class ListPagesController : ISearchable, IPortable
    {
        #region "Public Methods"

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// gets an object from the database 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="ModuleId">The Id of the module</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public PagesList GetListPagess(int ModuleId)
        {
            return (PagesList)CBO.FillObject(DataProvider.Instance().GetPagesList(ModuleId), typeof(PagesList));
        }

        public List<PageDetail> GetChildPageHtmlList(int ModuleId, int PageIndex, int PageSize)
        {
            PagesList listPagesInfo = GetListPagess(ModuleId);
            IDataReader dr = DataProvider.Instance().GetChildPageHtmlList(ModuleId, PageIndex, PageSize);

            List<PageDetail> pageDetails = new List<PageDetail>();

            while (dr.Read())
            {
                PageDetail pageDetail = new PageDetail();

                // 读取TabInfo
                int tabId = 0;
                if (dr["TabID"] != DBNull.Value && Int32.TryParse(dr["TabID"].ToString(), out tabId) && tabId != 0)
                {
                    pageDetail.CurrentTab = (new DotNetNuke.Entities.Tabs.TabController()).GetTab(tabId);
                }
                else
                {
                    continue;
                }

                // 读取页面HTML模块内容
                string htmlContent = string.Empty;
                if (dr["Content"] != DBNull.Value)
                {
                    htmlContent = dr["Content"].ToString();

                    // 解析Content内容

                    // 标题
                    pageDetail.Title = StringUtilily.GetTagInnerText(htmlContent, "h1");

                    // 描述文字
                    string description = StringUtilily.DropHtmlTag(htmlContent, "h1");
                    description = StringUtilily.ToTxt(description);

                    if (description.Length > listPagesInfo.DescriptionLength)
                    {
                        description = description.Substring(0, listPagesInfo.DescriptionLength);
                    }
                    pageDetail.Description = description + "...";

                    // 图片src
                    pageDetail.Imgsrc = StringUtilily.GetSrc(htmlContent);
                }
                else
                {
                    continue;
                }

                pageDetails.Add(pageDetail);

            }

            return pageDetails;
        }

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// adds an object to the database 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="objListPages">The ListPagesInfo object</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public void AddListPages(PagesList objListPages)
        {

            if (objListPages.Title.Trim() != "")
            {
                DataProvider.Instance().AddListPages(objListPages.ModuleId, objListPages.Title, objListPages.CreatedByUser);
            }

        }

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// saves an object to the database 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="objListPages">The ListPagesInfo object</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public void UpdateListPages(PagesList objListPages)
        {

            if (objListPages.Title.Trim() != "")
            {
                DataProvider.Instance().UpdateListPages(objListPages.ModuleId, objListPages.PageListId, objListPages.Title, objListPages.CreatedByUser);
            }

        }

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// deletes an object from the database 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="ModuleId">The Id of the module</param> 
        /// <param name="ItemId">The Id of the item</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public void DeleteListPages(int ModuleId, int ItemId)
        {

            DataProvider.Instance().DeleteListPages(ModuleId, ItemId);

        }

        #endregion

        #region "Optional Interfaces"

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// GetSearchItems implements the ISearchable Interface 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="ModInfo">The ModuleInfo for the module to be Indexed</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public DotNetNuke.Services.Search.SearchItemInfoCollection GetSearchItems(ModuleInfo ModInfo)
        {

            SearchItemInfoCollection SearchItemCollection = new SearchItemInfoCollection();

            List<PagesList> colListPagess = new List<PagesList>(); //GetListPagess(ModInfo.ModuleID);
            foreach (PagesList objListPages in colListPagess)
            {
                SearchItemInfo SearchItem = new SearchItemInfo(ModInfo.ModuleTitle, objListPages.Title, objListPages.CreatedByUser, objListPages.CreatedDate, ModInfo.ModuleID, objListPages.PageListId.ToString(), objListPages.Title, "ItemId=" + objListPages.PageListId.ToString());
                SearchItemCollection.Add(SearchItem);
            }

            return SearchItemCollection;

        }

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// ExportModule implements the IPortable ExportModule Interface 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="ModuleID">The Id of the module to be exported</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public string ExportModule(int ModuleID)
        {

            string strXML = "";

            List<PagesList> colListPagess = new List<PagesList>(); //GetListPagess(ModuleID);
            if (colListPagess.Count != 0)
            {
                strXML += "<ListPagess>";
                foreach (PagesList objListPages in colListPagess)
                {
                    strXML += "<ListPages>";
                    strXML += "<content>" + XmlUtils.XMLEncode(objListPages.Title) + "</content>";
                    strXML += "</ListPages>";
                }
                strXML += "</ListPagess>";
            }

            return strXML;

        }

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// ImportModule implements the IPortable ImportModule Interface 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="ModuleID">The Id of the module to be imported</param> 
        /// <param name="Content">The content to be imported</param> 
        /// <param name="Version">The version of the module to be imported</param> 
        /// <param name="UserId">The Id of the user performing the import</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public void ImportModule(int ModuleID, string Content, string Version, int UserId)
        {

            XmlNode xmlListPagess = Globals.GetContent(Content, "ListPagess");
            foreach (XmlNode xmlListPages in xmlListPagess.SelectNodes("ListPages"))
            {
                PagesList objListPages = new PagesList();
                objListPages.ModuleId = ModuleID;
                objListPages.Title = xmlListPages.SelectSingleNode("content").InnerText;
                objListPages.CreatedByUser = UserId;
                AddListPages(objListPages);
            }

        }

        #endregion

    }
}